/**
 * @desc
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    2017/3/10
 **/

let request = require('request');
let path = require('path');
let Immutable = require('immutable');
let fse = require('fs-extra');
let commonUtil = require('./util/common-util');

const url = 'http://sql2.dev.qianmi.com/api/sql/exec';

let allRecordPath = path.join(__dirname, "data/all-records.json");
let allUserDatas = require(allRecordPath);
let count = 0;
(async () => {
    let ids = Object.keys(allUserDatas);
    let length = Math.ceil(ids.length/100);
    for (let i = 0; i < length; i++) {
        let temp = ids.slice(i*100, (i+1)*100);
        let condition = "('" + temp.join('\',\'') + "')";
        let result = await getData(condition);
        console.log(result);
        if (result && result.data && result.data.length > 0) {
            let array = result.data;
            for (let j = 0; j < array.length ; j ++) {
                let id = array[j]['USER_ID'];
                if (allUserDatas[id]) {
                    allUserDatas[id].industry = array[j]['行业'] || '其它';
                    allUserDatas[id].manageType = array[j]['经营方式'] || '其它';
                    allUserDatas[id].signStatus = array[j]['签约状态'] === "2" ? '已签约' : '未签约';
                    if (array[j]['签约状态'] === "2") {
                        count++;
                    }
                }
            }
        }
    }

    console.log('已签约：'+count);
    console.log('索引数据结束');
    //保存到本地数据中去.
    fse.outputJson(allRecordPath, allUserDatas);
    commonUtil.generateUserDimension(allUserDatas);
})()


function getData (condition) {
    let sql = "select user_id, SIGN_STATUS 签约状态, " +
        "(SELECT dname FROM QMUC.QM_DICT_TAB c " +
        "where dtype = 'industry_type' and c.DVALUE = t_ua.job_type) 行业," +
        "(select dname from QMUC.QM_DICT_TAB c " +
        "where dtype = 'business_range_type' and c.DVALUE = t_ua.manage_type) 经营方式 " +
        "from qmuc.tb_user_admininfo t_ua where user_id in " +
        condition;
    return new Promise((resolve,reject)=>{
        request.post(url, {
            formData: {
                iid: "d6e818bb91aafff83e19b4466f1f360f",
                sql: sql,
                src: sql,
                dbtype: "oracle",
                token: "of10811489117519164",
                pageIndex: 0,
                pageSize: 100
            }, jar: getCookieJar()
        }, function (err, res, body) {
            if (!err && res.statusCode === 200) {
                resolve(JSON.parse(body));
            } else {
                reject({});
            }
        })
    })
}

function getCookieJar(){
    var jar = request.jar();

    // jar.setCookie(request.cookie('a9a68f4fefd3b693f10be4a89799dc48=57357846d8e6477aa2b4aae4ef29ff73'), url);
    // jar.setCookie(request.cookie('__sso_key=5755feb5017a5affdc415964939abe8e'), url);
    // jar.setCookie(request.cookie('SSOTICKET=mReYZshkizCQda7CTl8wlOQ0EaullNQIZxZIPbZRQppR31AO7_Jnjzn8foFLC_orPsnI8R2qsRSpNwBbkXxEFN2XHnAmCc6Ef58Yrjky8atjMQaGRdbsa-7p4JgQhnSCIzUh7oFRzPZP22_JiNQC8QBuSnGrl27YyrwlXN_KI3WkRl5yfhRViklBbfelheTOF00y3XocSicI7v-Ov6N8FQ..'), url);
    // jar.setCookie(request.cookie('admin_abtest=1'), url);
    // jar.setCookie(request.cookie('qmstore_abtest=0'), url);
    // jar.setCookie(request.cookie('cxuid=1078241'), url);
    // jar.setCookie(request.cookie('token=of10811489117952697'), url);
    // jar.setCookie(request.cookie('_ga=GA1.2.128862619.1489110974'), url);
    // jar.setCookie(request.cookie('__sso_key=5755feb5017a5affdc415964939abe8e'), url);
    // jar.setCookie(request.cookie('connect.sid=s%3AhEFmzg7KfuSvDZFZhAt29eCxT2FlZNaR.%2Bseo3sLSSl8HTg8PzjWl5q76vHT6A%2BO6irZcG0JoY0s'), url);
    // jar.setCookie(request.cookie('SSOEXPIRES=MjAxNy0wMy0xMCAxMjowNjoyNw..'), url);
    // jar.setCookie(request.cookie('SSOTHRESHOLD=NQ..'), url);
    //
    //
    jar.setCookie(request.cookie('token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkIjoiMTA1IiwiaSI6IiIsIm0iOiJ5YW5neGlhb2RvbmdAcWlhbm1pLmNvbSIsIm4iOiLmnajmmZPkuJwiLCJyIjoiOSwxMCIsInMiOiJvZjEwODEifQ.QW5LJrssWu0eb5kDHybLPEFB3bGFYBrO_Bay-33XwTc'), url);


    return jar;
}
